package com.homework.T5;

import java.util.ArrayList;

/**
 * @author xiyu
 * @version 1.0
 * @date 2025/3/29 19:07
 * @description
 */
public class Test {
    public static void main(String[] args) {
        //牌组
        ArrayList<Card> cards = new ArrayList<>();
        //三个玩家的牌
        ArrayList<Card> cards1 = new ArrayList<>();
        ArrayList<Card> cards2 = new ArrayList<>();
        ArrayList<Card> cards3 = new ArrayList<>();
        //印牌
        getcards(cards);
        //地主牌
        int dizhu = (int) (Math.random()*(54-1+1))+1;
        //发51次牌+三张地主
        int[] x = new int[54-3];
        //发牌
        int count = 0;
        for (int i = 0; i < x.length; i++) {
            int c = (int) (Math.random()*(54-1+1))+1;
            boolean flag = true;
            for (int x1 : x) {
                if (c==x1){
                    flag = false;
                    i--;
                    break;
                }
            }
            if (flag){
                x[i] = c;
                Card temp = new Card();
                for (Card card : cards) {
                    if (card.getCid() == c){
                        temp.setCid(card.getCid());
                        temp.setDianshu(card.getDianshu());
                        temp.setHuase(card.getHuase());
                    }
                }
                count++;
                switch (count){
                    case 1:
                        cards1.add(temp);
                        break;
                    case 2:
                        cards2.add(temp);
                        break;
                    case 3:
                        cards3.add(temp);
                        count=0;//重置发牌顺序
                        break;
                }
            }
        }
        //找剩下的三张牌
        int[] c3 = new int[3];
        int cindex = 0;
        for (int i = 0; i < cards.size(); i++) {
            boolean flag = true;
            for (int j = 0; j < x.length; j++) {
                if (cards.get(i).getCid()==x[j]){
                    flag = false;
                    break;
                }
            }
            if (flag){
                c3[cindex] = cards.get(i).getCid();
                cindex++;
            }
        }
//        System.out.println("c3"+Arrays.toString(c3));

        ArrayList<Player> players = new ArrayList<>();
        players.add(new Player(1,"李大","农民",cards1));
        players.add(new Player(2,"李二","农民",cards2));
        players.add(new Player(3,"李三","农民",cards3));
        //找地主
        for (Player player : players) {
            if (findDizhu(player,dizhu)){
                player.setShenfen("地主");
                for (Card card : cards) {
                    for (int i : c3) {
                        if (i==card.getCid()){
                            player.getCards().add(card);
                        }
                    }
                }
            }
        }

        for (Player player : players) {
            System.out.println("player = " + player);
            System.out.println("---------------------------");
        }

//        System.out.println(cards1.size());
//        System.out.println(cards2.size());
//        System.out.println(cards3.size());
    }

    private static boolean findDizhu(Player player, int dizhu) {
        ArrayList<Card> cards = player.getCards();
        for (Card card : cards) {
            if (card.getCid()==dizhu){
                return true;
            }
        }
        return false;
    }

    private static void getcards(ArrayList<Card> cards) {
        int cid = 1;
        int dianshu = 1;
        while (true) {
            String huase = "";
            if (cid>0&&cid<=13){
                huase = "红桃";
            }else if (cid>13&&cid<=26){
                huase = "黑桃";
            }else if (cid>26&&cid<=39){
                huase = "方块";
            }else if (cid>39&&cid<=52){
                huase = "梅花";
            }else if (cid==53){
                huase = "小王";
            }else if (cid==54){
                huase = "大王";
            }
                cards.add(new Card(huase,dianshu,cid));
            dianshu++;
            cid++;
            if (dianshu==14){
                dianshu=1;
            }
            if (cid==55){
                break;
            }
        }
    }
}
